#include "SQLColumnCheck.h"

#include "../../../sql/visitor/SQLASTVisitor.h"

SQLColumnCheck::SQLColumnCheck()
{
}

SQLColumnCheck::SQLColumnCheck(SQLExpr_ptr expr)
{
  this->setExpr(expr);
}

SQLExpr_ptr SQLColumnCheck::getExpr()
{
  return expr;
}

void SQLColumnCheck::setExpr(SQLExpr_ptr expr)
{
  if (expr != NULL)
  {
    expr->setParent(SharedObject(SQLColumnCheck));
  }
  this->expr = expr;
}

BOOL_ptr SQLColumnCheck::getEnforced()
{
  return enforced;
}

void SQLColumnCheck::setEnforced(BOOL_ptr enforced)
{
  this->enforced = enforced;
}

void SQLColumnCheck::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLColumnCheck)))
  {
    acceptChild(visitor, this->getName());
    acceptChild(visitor, this->getExpr());
  }
  visitor->endVisit(SharedObject(SQLColumnCheck));
}

SQLObject_ptr SQLColumnCheck::clone()
{
  SQLObject_ptr clone_tmp = SQLColumnCheck_ptr(new SQLColumnCheck());
  SQLColumnCheck_ptr x = std::dynamic_pointer_cast<SQLColumnCheck>(clone_tmp);
  // SQLColumnCheck *x = new SQLColumnCheck();

  SQLConstraintImpl::cloneTo(std::dynamic_pointer_cast<SQLConstraintImpl>(x));

  if (expr != NULL)
  {
    x->setExpr(std::dynamic_pointer_cast<SQLExpr>(expr->clone()));
  }

  return x;
}

BOOL_ptr SQLColumnCheck::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (this->expr == expr)
  {
    setExpr(target);
    return BOOL::TRUE;
  }

  if (getName() == expr)
  {
    setName(std::dynamic_pointer_cast<SQLName>(target));
    return BOOL::TRUE;
  }

  if (getComment() == expr)
  {
    setComment(target);
    return BOOL::TRUE;
  }
  return BOOL::FALSE;
}
